package com.stephentuso.welcome;

import com.stephentuso.welcome.util.PageView;
import ohos.agp.components.Component;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.StackLayout;
import ohos.agp.components.Text;
import ohos.agp.render.Canvas;
import ohos.agp.utils.Color;
import ohos.app.Context;
import ohos.utils.PacMap;

/**
 * Created by stephentuso on 1/23/16.
 */
public class WelcomeParallaxPageView extends PageView implements WelcomePage.OnChangeListener, Component.DrawTask {

    public static final String KEY_LAYOUT_ID = "drawable_id";
    public static final String KEY_DESCRIPTION = "description";
    public static final String KEY_TITLE = "title";
    public static final String KEY_START_FACTOR = "start_factor";
    public static final String KEY_END_FACTOR = "end_factor";
    public static final String KEY_PARALLAX_RECURSIVE = "parallax_recursive";
    public static final String KEY_HEADER_TYPEFACE_PATH = "header_typeface";
    public static final String KEY_DESCRIPTION_TYPEFACE_PATH = "description_typeface";
    public static final String KEY_HEADER_COLOR = "header_color";
    public static final String KEY_DESCRIPTION_COLOR = "description_color";

    private StackLayout frameLayout = null;
    private Text titleView = null;
    private Text descriptionView = null;

    private float startFactor = 0.2f;
    private float endFactor = 1.0f;
    private float parallaxInterval = 0f;
    private boolean parallaxRecursive = false;

    public static WelcomeParallaxPageView newInstance(Context context, int layoutId,
                                                      String title,
                                                      String description,
                                                      float startParallaxFactor,
                                                      float endParallaxFactor,
                                                      boolean parallaxRecursive,
                                                      String headerTypefacePath,
                                                      String descriptionTypefacePath,
                                                      int headerColor,
                                                      int descriptionColor)
    {
        PacMap args = new PacMap();
        args.putIntValue(KEY_LAYOUT_ID, layoutId);
        args.putString(KEY_TITLE, title);
        args.putString(KEY_DESCRIPTION, description);
        args.putFloatValue(KEY_START_FACTOR, startParallaxFactor);
        args.putFloatValue(KEY_END_FACTOR, endParallaxFactor);
        args.putBooleanValue(KEY_PARALLAX_RECURSIVE, parallaxRecursive);
        args.putString(KEY_HEADER_TYPEFACE_PATH, headerTypefacePath);
        args.putString(KEY_DESCRIPTION_TYPEFACE_PATH, descriptionTypefacePath);
        args.putIntValue(KEY_HEADER_COLOR, headerColor);
        args.putIntValue(KEY_DESCRIPTION_COLOR, descriptionColor);
        WelcomeParallaxPageView pageView = new WelcomeParallaxPageView(context, args);
        return pageView;
    }

    public WelcomeParallaxPageView(Context context, PacMap args) {
        super(context);
        Component component = LayoutScatter.getInstance(context).parse(ResourceTable.Layout_wel_page_view_parallax, this, true);

        frameLayout = (StackLayout) component.findComponentById(ResourceTable.Id_wel_parallax_frame);
        titleView = (Text) component.findComponentById(ResourceTable.Id_wel_title);
        descriptionView = (Text) component.findComponentById(ResourceTable.Id_wel_description);

        startFactor = args.getFloatValue(KEY_START_FACTOR, startFactor);
        endFactor = args.getFloatValue(KEY_END_FACTOR, endFactor);
        parallaxRecursive = args.getBooleanValue(KEY_PARALLAX_RECURSIVE, parallaxRecursive);

        LayoutScatter.getInstance(context).parse(args.getIntValue(KEY_LAYOUT_ID), frameLayout, true);

        if (args.getString(KEY_TITLE) != null)
            titleView.setText(args.getString(KEY_TITLE));

        if (args.getString(KEY_DESCRIPTION) != null)
            descriptionView.setText(args.getString(KEY_DESCRIPTION));

        int headerColor = args.getIntValue(KEY_HEADER_COLOR, WelcomeUtils.NO_COLOR_SET);
        if (headerColor != WelcomeUtils.NO_COLOR_SET)
            titleView.setTextColor(new Color(headerColor));

        int descriptionColor = args.getIntValue(KEY_DESCRIPTION_COLOR, WelcomeUtils.NO_COLOR_SET);
        if (descriptionColor != WelcomeUtils.NO_COLOR_SET)
            descriptionView.setTextColor(new Color(descriptionColor));

        WelcomeUtils.setTypeface(titleView, args.getString(KEY_HEADER_TYPEFACE_PATH), getContext());
        WelcomeUtils.setTypeface(descriptionView, args.getString(KEY_DESCRIPTION_TYPEFACE_PATH), getContext());

        this.addDrawTask(this);
    }

    @Override
    public void onDraw(Component component, Canvas canvas) {
        parallaxInterval = (endFactor - startFactor)/(WelcomeUtils.calculateParallaxLayers(frameLayout.getComponentAt(0), parallaxRecursive) - 1);
    }

    @Override
    public void onWelcomeScreenPageScrolled(int pageIndex, float offset, int offsetPixels) {
        /*if (frameLayout != null) {
            WelcomeUtils.applyParallaxEffect(frameLayout.getComponentAt(0), parallaxRecursive, offsetPixels, startFactor, parallaxInterval);
        }*/
    }

    @Override
    public void onWelcomeScreenPageSelected(int pageIndex, int selectedPageIndex) {
        //Not used
    }

    @Override
    public void onWelcomeScreenPageScrollStateChanged(int pageIndex, int state) {
        //Not used
    }

}
